package com.globallogic.socialnetwork.jsf.bean;

import javax.ejb.EJB;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.context.annotation.Scope;

import com.globallogic.socialnetwork.entity.User;
import com.globallogic.socialnetwork.service.UserService;
import com.globallogic.socialnetwork.util.DaoException;
import com.globallogic.socialnetwork.util.WebApplication;

/**
 * JSF Bean to handle login operations
 */
@Named("regBean")
@Scope("session")
public class RegistrationBean {

	@EJB(mappedName="java:module/UserService")
	private UserService userService;
	@Length(min = 3, max = 25, message = "Length must be in range 3-25")
	private String login;
	@Length(min = 6, max = 25, message = "Length must be in range 6-25")
	private String password;
	@Length(min = 6, max = 25, message = "Length must be in range 6-25")
	private String confirmPassword;
	@NotEmpty
	private String name;
	private String sex;
	private String city;
	private String userInfo;

	private final static Logger log = Logger
			.getLogger("SocialNetworkFileLogger");

	public RegistrationBean() {
	}

	/**
	 * Registers user with parameters
	 * <login>,<password>,<confirmPassword>,<name>
	 * 
	 * @return empty string
	 */
	public String registration() {
		if (password.equals(confirmPassword)) {
			User newUser = new User(login, password, name, sex, city, userInfo);
			try {
				if (userService.register(newUser)) {
					System.out.println(newUser);
					try{
					WebApplication.addMessage("registration successfull");
					}catch (NullPointerException e) {
					}
					return "login page";
				} else {
					try {
						WebApplication
								.addMessage("Unable to register new user. The user with name "
										+ newUser.getName()
										+ " already exists.");
					} catch (NullPointerException e) {
					}
				}
				clearForm();
			} catch (DaoException e) {
				log.warn("");
			}
			return "";
		} else {
			try {
				WebApplication.addMessage("Passwords don't match.");
			} catch (NullPointerException e) {
			}
			return "";
		}
	}

	/**
	 * Clears form for registration
	 */
	public void clearForm() {
		login = "";
		password = "";
		confirmPassword = "";
		name = "";
		sex = "";
		city = "";
		userInfo = "";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public String getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(String userInfo) {
		this.userInfo = userInfo;
	}

}
